app.controller("addupdate",function($scope,$http, $stateParams,get,post,$rootScope,Md5,$state){
	$scope.sexList=[{id:-1,name:"请选择"},{id:0,name:"男"},{id:1,name:'女'}]
	$scope.educationList=[{id:-1,name:"请选择"},{id:0,name:"其他"},{id:1,name:'小学'}
						,{id:2,name:"初中"},{id:3,name:"高中"},{id:4,name:'职高'}
						,{id:5,name:"中专"},{id:6,name:"大专"},{id:7,name:'本科'}
						,{id:8,name:"研究生"},{id:9,name:"硕士"},{id:10,name:'博士'}
						,{id:11,name:'博士后'}]
	$scope.maritalStatusList=[{id:-1,name:"请选择"},{id:0,name:"其他"},{id:1,name:'未婚'},{id:2,name:'已婚'},{id:3,name:'离异'}]
	
	
	//所属部门
			$scope.did={
						value:{
							diName:"请选择",
							diId:null
						}
					}
			//工作状态
			$scope.status={
						value:{
							diName:"请选择",
							diId:null
						}
					}
			//专业
			$scope.profession={
						value:{
							diName:"请选择",
							diId:null
						}
					}
			//户口性质
			$scope.householdRegister={
						value:{
							diName:"请选择",
							diId:null
						}
					}		
	
	
	
	
	if($stateParams.id!=''){
		get.list('staff/findOne',{sId:$stateParams.id}).then(function(e){
		$scope.pickertime=e.sEntryTime
		$scope.pickertimes=e.sWorking
			$scope.add={
		id:e.sId,
		serialNum:e.sSerialNum,
		name:e.sName,
		email:e.sEmail,
		telephone:e.sTelephone,
		officPhone:e.sOfficePhone,
		entryTime:e.sEntryTime,
		working:e.sWorking,
		did:e.departmentId,
		status:e.sStatus,
		user:e.user.uAccount||'',
		password:e.user.uPassword,
		qpassword:e.user.uPassword,
		cardId:e.staffArchive.saCardId,
		sex:e.staffArchive.saSex,
		education:e.staffArchive.saEducation,
		master:e.staffArchive.saMaster,
		profession:e.staffArchive.saProfession||'',
		maritalStatus:e.staffArchive.saMaritalStatus,
		nativePlace:e.staffArchive.saNativePlace,
		householdRegister:e.staffArchive.saHouseholdRegister||'',
		nation:e.staffArchive.saNation,
		familyAddress:e.staffArchive.saFamilyAddress,
		work:e.staffArchive.saWork,
		dIds:null,
		position:null
	}
	
			
			//所属部门
	angular.forEach($scope.department,function(o){
		if(e.departmentId==o.diId){
			
			$scope.did={
				value:o
			}
		}
	})
	//工作状态
	angular.forEach($scope.service,function(o){
		if(e.sStatus==o.diId){
			$scope.status={
					value:o
			}
	
		}
	})
	//专业
	angular.forEach($scope.professional,function(o){
		if(e.staffArchive.saProfession==o.diId){
			
			$scope.profession={
					value:o
			}
		}
	})
		//户口性质
	angular.forEach($scope.character,function(o){
		if(e.staffArchive.saHouseholdRegister==o.diId){
			$scope.householdRegister={
				value:o
			}
		}
	})
	var dIds=e.diIds.split(",");
	//岗位
	angular.forEach($rootScope.post,function(o){
		angular.forEach(dIds,function(os){
				if(os==o.diId){
					o.checked=true
				}
		})
		
	})
		})
	}
	
	
	$scope.add={
		serialNum:null,
		name:null,
		email:null,
		telephone:null,
		officPhone:null,
		pickertime:null,
		pickertimes:null,
		did:null,
		status:null,
		user:null,
		password:null,
		qpassword:null,
		cardId:null,
		sex:-1,
		education:-1,
		master:null,
		profession:-1,
		maritalStatus:null,
		nativePlace:null,
		householdRegister:null,
		nation:null,
		familyAddress:null,
		work:null,
		dIds:null,
		position:null
	}
	$scope.sub=function(val){
		var position=''
		var dIds=''
		angular.forEach($scope.post,function(o){
			if (o.checked==true) {
				 position+=o.diName+","
			     dIds+=o.diId+","
			} 
		})
		/*$scope.status.value=$scope.add.status
		$scope.did.value=$scope.add.did
		$scope.profession.value=$scope.add.profession
		$scope.householdRegister.value=$scope.add.householdRegister*/
		angular.forEach($scope.staff,function(o,vals){
			if(val==vals){
				$scope.staff[vals]=true
			}else{
				$scope.staff[vals]=false
			}
		})
		$scope.add.dIds=dIds
		$scope.add.position=position
		/*$scope.add.entryTime=$scope.pickertime
		$scope.add.working=$scope.pickertimes*/
		$scope.add.did=$scope.add.did.diId||$scope.add.did
		$scope.add.status=$scope.add.status.diId||$scope.add.status
		if(val=="sub"){
			if($scope.pwd){
				$scope.add.password=Md5.hex_md5($scope.add.password)
				$scope.add.qpassword=Md5.hex_md5($scope.add.qpassword)
			}
			if($scope.add.profession!=null){
				$scope.add.profession=$scope.add.profession.diId||$scope.add.profession
			}
			if($scope.add.householdRegister!=null){
				$scope.add.householdRegister=$scope.add.householdRegister.diId||$scope.add.householdRegister
			}
			
			if($stateParams.id!=''){
				post.list("staff/update",$scope.add).then(function(o){
						$state.go("home."+$stateParams.url,{page:$stateParams.page},{
						     		reload:true
						     	})
				})
			}else{
				
				post.list("staff/add",$scope.add).then(function(o){
						$state.go("home."+$stateParams.url,{page:$stateParams.page},{
						     		reload:true
						     	})
				})
				
			}
			
			
		}
		
		
	}
	
/*	 $scope.$watch('add.password', function (value) {
		 	if (!value)return;
					 $scope.add.password=Md5.hex_md5(value)
       })
	*/
	$scope.password=function(){
		if( $scope.add.password!=null){
			$scope.pwd=true
		}
		
	}
	if($stateParams.id==''){
		$scope.$watch("add.user",function  (val) {
			if(!val)return;
				get.list("user/account",{uAccount:val}).then(function(e){
					 $scope.register=e
				})
                 
                })
	}
	
	$scope.staff={
		staff:true,
		login:false,
		activen:false
	}
	$scope.isshow=function(val){
		angular.forEach($scope.staff,function(o,vals){
			if(val==vals){
				$scope.staff[vals]=true
			}else{
				$scope.staff[vals]=false
			}
		})
	}
	
})
